<?php

namespace app\api\controller\yexam;

use app\common\controller\Api;
use think\Db;
/**
 * 班次--自定义的
 * @ApiInternal
 */
class Package extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 获取科目列表
     */
    public function index()
    {
        $page = $this->request->get('page') ?? 1;
        $limit = $this->request->get('limit') ?? 100;
        $subjectId = $this->request->get('subject_id');

        $subject = \app\admin\model\yexam\Subject::get($subjectId);
        $package_ids = $subject->package_ids;
        $item = $subject->toArray();

        $package = new \app\admin\model\Package;

        // content_json to content
        $item['package_content'] = [];
        if ($item['package_ids']) {
            $package_ids = explode(',', $item['package_ids']);
            $item['package_content'] = collection($package->where('id', 'in', $package_ids)->select())->toArray();
        }

        if ($item['content_json']) {
            $item['content'] = json_decode($item['content_json']);
        } else {
            $item['content'] = null;
        }
        unset($item['content_json']);

        $this->success('请求成功', $item);
    }
	
	 public function ename()
    {
        $ename = $this->request->get('ename');
		if(!$ename) $this->error('错误');
        $subject = \app\admin\model\yexam\Subject::get(['en_name'=>$ename]);
		if($subject){
			$this->success('请求成功', $subject);
		}else{
			$this->error('请求失败');
		}
        
    }
	
	//热门班次
	public function hot(){
		$page = $this->request->get('page') ?? 1;
        $limit = $this->request->get('limit') ?? 100;
        $subjectId = $this->request->get('subject_id');

        $subject = \app\admin\model\yexam\Subject::get($subjectId);
		$this->model = new \app\common\model\Category;
		$row = $this->model->get($subject['category_id']);
        $item = $subject->toArray();
		//var_dump($row);
		$package = new \app\admin\model\Package;
		//$item['package_content'] = [];
		$amap[] =['exp', Db::raw("FIND_IN_SET('hot',flag)")];
		$item['package_content'] = collection($package->where('category_id',$row['pid'])->where($amap)->select())->toArray();
		
		$this->success('请求成功', $item);
	}
	
	public function phot(){
		$page = $this->request->get('page') ?? 1;
        $limit = $this->request->get('limit') ?? 100;
        $categoryId = $this->request->get('category_id');

        $this->model = new \app\common\model\Category;
		$row = $this->model->get($categoryId);
		$package = new \app\admin\model\Package;
		//$item['package_content'] = [];
		//var_dump($row);
		$amap[] =['exp', Db::raw("FIND_IN_SET('hot',flag)")];
		$item['package_content'] = collection($package->where('category_id',$row['pid'])->where($amap)->select())->toArray();
		
		
		$this->success('请求成功', $item);
	}
	
	

}
